﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using Microsoft.Owin.Security.OAuth;
using Newtonsoft.Json.Serialization;
using System.Net.Http.Formatting;
using CacheCow.Server;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Caltech
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            // Add caching handler
            //var cacheHandler = new CachingHandler(config);
            //config.MessageHandlers.Add(cacheHandler);

            var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();

            // support json camelcase
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            // Create Json.Net formatter serializing DateTime using the local timezone
            jsonFormatter.SerializerSettings.DateTimeZoneHandling  = DateTimeZoneHandling.Local;
            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
        }
    }
}
